<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Feedback as FeedModel;
use app\common\model\FeedbackComment;
use app\common\model\FeedbackCommentReply;
use app\common\model\FeedbackLike;

class Feedback extends Api{

    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 问题反馈列表
     */
    public function lists(){
        $type = request()->post('type',1);// 1=按时间,2=按点赞
        $page = request()->post('page',1);
        $limit = request()->post('limit',config('paginate.list_rows'));
        if($type == 1){
            $field = 'created_at';
        }else{
            $field = 'like_num';
        }
        $list = FeedModel::with('comment,comment.user,user')->where('status',1)->order($field,'desc')->page($page,$limit)->select();
        $this->success('请求成功',$list);
    }

    /**
     * 反馈详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail(){
        $fId = request()->post('f_id');
        if(empty($fId)){
            $this->error('参数错误');
        }
        $feedInfo = FeedModel::with('user')->where('id',$fId)->where('status',1)->find();
        if(empty($feedInfo)){
            $this->error('参数错误');
        }
        $this->success('请求成功',$feedInfo);
    }

    /**
     * 添加问题反馈
     */
    public function add(){
        $post = request()->post();
        $result = $this->validate($post,'FeedbackValidate');
        if($result !== true){
            $this->error($result);
        }
        $image = explode(',',$post['image']);
        if(count($image) > 9){
            $this->error('图片最多上传9张');
        }

        FeedModel::create([
            'content'   => $post['content'],
            'images'    => $post['image'],
            'user_id'   => $this->auth->id
        ]);
        $this->success('反馈成功');
    }

    /**
     * 问题反馈点赞
     * @throws \think\Exception
     */
    public function like(){
        $user_id = $this->auth->id;
        $f_id = $this->request->post('f_id');
        $att = FeedbackLike::where('user_id',$user_id)->where('f_id',$f_id)->count();
        if($att > 0){
            return $this->error('您已点过赞');
        }
        FeedbackLike::create(['user_id'=>$user_id,'f_id'=>$f_id]);
        FeedModel::where('id',$f_id)->setInc('like_num',1);
        return $this->success('请求成功');
    }

    /**
     * 添加评论
     */
    public function addComment(){
        $post = request()->post();
        if(!isset($post['content']) || empty($post['content'])){
            $this->error('参数错误');
        }
        if(isset($post['c_id']) && !empty($post['c_id'])){
            // 回复
            $commentCount = FeedbackComment::where('id',$post['c_id'])->count();
            if($commentCount < 1){
                $this->error('参数错误!');
            }
            $data = [
                'c_id'   => $post['c_id'],
                'content'=> $post['content'],
                'user_id'=> $this->auth->id,
            ];
            if(isset($post['reply_id']) && !empty($post['reply_id'])){
                $reply = FeedbackCommentReply::where('id',$post['reply_id'])->field('id,user_id')->find();
                if($reply['user_id'] == $this->auth->id){
                    $this->error('无法回复自己');
                }
                if(!empty($reply)){
                    $data['to_user_id'] = $reply['user_id'];
                    $data['reply_id'] = $post['reply_id'];
                }
            }
            $result = FeedbackCommentReply::create($data);
            if($result){
                $this->success('回复成功');
            }
            $this->error('回复失败');
        }else{
            // 评论
            if(!isset($post['f_id']) || empty($post['f_id'])){
                $this->error('参数错误');
            }
            $feedCount = FeedModel::where('id',$post['f_id'])->where('status',1)->count();
            if($feedCount < 1){
                $this->error('参数错误');
            }
            $result = FeedbackComment::create([
                'f_id'      => $post['f_id'],
                'user_id'   => $this->auth->id,
                'content'   => $post['content']
            ]);
            if($result){
                $this->success('评论成功');
            }
            $this->error('评论失败');
        }
    }

    /**
     * 评论列表
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function commentList(){
        $fId = request()->post('f_id');
        $page = request()->post('page',1);
        $limit = request()->post('limit',config('paginate.list_ros'));
        $feedCount = FeedModel::where('id',$fId)->where('status',1)->count();
        if($feedCount < 1){
            $this->error('参数错误');
        }
        $list = FeedbackComment::with(['user','reply'=>function($query){
            return $query->with('user,toUser');
        }])->where('f_id',$fId)->order('created_at','asc')->page($page,$limit)->select();
        $this->success('请求成功',$list);
    }
}